/***
This file generates Covenant measures for the Compustat-Dealscan dataset.

NOTE: Credit for the creation of the covenant strictnness measure and that creates it below goes to Justin Murfin. If used, please cite the paper: "The Supply-Side Determinants of Loan Contract Strictness (THE JOURNAL OF FINANCE, 2012)"

File inputs: 
- comp_fundq6.dta (Compustat quarterly data, created in `0_2_Sample_Creation_Quarterly.sas')
- comp_covenants2.dta (Created in Part a of this Step)

File outputs:
- COMP_Q_dttd_violations.dta: The dataset with the covenant violations and slackness measures (at the quarterly level.)
- COMP_A_dttd_violations.dta: The dataset with the covenant violations and slackness measures (at the annual level.)

***/


global dir your_path
cd "$dir"

*** This code start with the full Compustat (quarterly sample) --- See "0_2_Sample_Creation_Quarterly.sas"

use "comp_fundq6", clear

gen year=year(datadate)
gen q=quarter(datadate)

keep if year>=1990
destring sic, replace
gen sic1=int(sic/1000)
destring gvkey, gen(gvkey2)
drop if year>=2020

sort gvkey2 year q
drop if gvkey2==gvkey2[_n+1] & year==year[_n+1] & q==q[_n+1]
gen date2=yq(year,q)
format date2 %tq
xtset gvkey2 date2


* Generate logs of all variables 
gen ln_capxq4=ln(capxq4)
gen ln_ebitda_debt=ln(ebitda_debt)
gen ln_debt_equity=ln(debt_equity)
gen ln_debt_tangnetworth=ln(debt_tangnetworth)
gen ln_int_cov=ln(int_cov)
gen ln_cur_ratio=ln(cur_ratio)
gen ln_ebitda=ln(ebitda)
gen ln_fx_ch_cov=ln(fx_ch_cov)
gen ln_quick_ratio=ln(quick_ratio)
gen ln_tangnetworth=ln(tangnetworth)
gen ln_networth2=ln(networth2)
gen ln_bl=ln(1+bl)

** ALL COVENANTS ***

*** I changed this part: Look for it to replace back to normal.
gen d1=d.ln_capxq4 		 	 // gen d1=d.Capex 
gen d2=d.ln_ebitda_debt 	//gen d2=d.EBITDA_to_Debt
gen d3=d.ln_debt_equity		// gen d3=d.Debt_to_Equity 
gen d4=d.ln_debt_tangnetworth		// gen d4=d.Debt_to_Tangible_Net_Worth  
gen d5=d.ln_int_cov		// gen d5=d.Interest_Coverage 
gen d6=d.ln_cur_ratio		// gen d6=d.Current_Ratio
gen d7=d.ln_ebitda		// gen d7=d.EBITDA
gen d8=d.ln_fx_ch_cov		// gen d8=d.Fixed_Charge_Coverage 
gen d9=d.ln_quick_ratio		// gen d9=d.Quick_Ratio 
gen d10=d.ln_tangnetworth		// gen d10=d.Tangible_Net_Worth 
gen d11=d.ln_networth2		// gen d11=d.Net_Worth
egen h=rownonmiss(d1-d11) 
drop if atq==.
drop if h<11
preserve

*** 
set more off
forvalues i = 0/9 {
forvalues y = 1994/2019 {
keep if sic1==`i'
keep if year<`y'
qui corrmat d1-d11,  covmat(covmat`i'_`y') 
restore
preserve
        }
		}
clear
timer off 1
timer list 1


gen id=_n
save "temp", replace

**** USE covenant slackness data to get strictness  ; 
use "comp_covenants2", clear

gen year=year(datadate)
keep if year>=1994
foreach x in slack_int_cov slack_cur_ratio slack_ebitda_debt slack_debt_tangnetworth slack_fx_ch_cov slack_bl slack_quick_ratio slack_debt_equity slack_capex slack_ebitda slack_tangnetworth slack_net_worth {
	replace `x'=0 if `x'<0 & `x'!=.
}
gen id=_n
destring sic, replace
gen sic4=sic
drop sic
gen sic=int(sic4/1000)

* Install mvnp in STATA use use the mdraws function and mvnp function below.  I've set dr to 500, but the integration will be more accurate as the number of draws gets bigger.
mdraws, neq(11) dr(100) prefix(pre) random seed(123456)
save "temp2", replace

tab sic 

*This generates the occasional problem of a non-positive definite matrix (primarily due to (NW and TNW being the same prior to the dot com boom).
set more off
timer on 2
forvalues i = 0/9 {
*forvalues y = 1991/2014 {
 keep if sic==`i'
 *keep if year==`y'
capture mat c=cholesky(covmat`i'_2018)
egen prv1=mvnp(slack_int_cov slack_cur_ratio slack_ebitda_debt slack_debt_tangnetworth slack_fx_ch_cov ///
			   slack_quick_ratio slack_debt_equity  slack_tangnetworth slack_net_worth), ///
				dr(100) chol(c) prefix(pre) adoonly 
keep id prv1
sort id
qui merge 1:1 id using "temp", 
drop _merge
mat drop c
save "temp", replace
use "temp2", replace
		 }
		 *}
drop pre*
merge 1:1 id using "temp" 
erase "temp.dta"
erase "temp2.dta"
 restore, not
gen dttd=(1-prv1)*100
drop if gvkey==""

gen dttd2=dttd 

gen ln_dttd2=ln(dttd2)


destring(gvkey), replace


*** Save the dataset: 
save "COMP_Q_dttd_violations", replace



*** Convert the data to annual and merge them with Billy's Compustat File: 
use  "COMP_Q_dttd_violations.dta", clear 

collapse (max) dttd dttd2 viol_confirmed (min) slack_* (mean) dttd_mean=dttd, by(gvkey year)
gen fyear=year

save "COMP_A_dttd_violations.dta", replace


